﻿using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Win32;

namespace RegistryHelper
{
    public enum RootKey
    {
        HKEY_CLASSES_ROOT,
        HKEY_CURRENT_USER,
        HKEY_LOCAL_MACHINE,
        HKEY_USERS,
        HKEY_CURRENT_CONFIG,
        HKEY_DYNDATA,
        HKEY_PERFORMANCEDATA
    }

    public class RegistryUtils
    {
        private static RegistryKey rootkey;

        public RegistryUtils()
            : this(RootKey.HKEY_LOCAL_MACHINE)
        {
        }

        public RegistryUtils(RootKey rootKey)
        {
            switch (rootKey)
            {
                case RootKey.HKEY_CLASSES_ROOT:
                    rootkey = Registry.ClassesRoot;
                    break;
                case RootKey.HKEY_CURRENT_USER:
                    rootkey = Registry.CurrentUser;
                    break;
                case RootKey.HKEY_LOCAL_MACHINE:
                    rootkey = Registry.LocalMachine;
                    break;
                case RootKey.HKEY_USERS:
                    rootkey = Registry.Users;
                    break;
                case RootKey.HKEY_CURRENT_CONFIG:
                    rootkey = Registry.CurrentConfig;
                    break;
                case RootKey.HKEY_DYNDATA:
                    rootkey = Registry.DynData;
                    break;
                case RootKey.HKEY_PERFORMANCEDATA:
                    rootkey = Registry.PerformanceData;
                    break;
                default:
                    rootkey = Registry.LocalMachine;
                    break;
            }
        }

        public string GetRegVal(string keypath, string keyname, string def)
        {
            try
            {
                RegistryKey key = rootkey.OpenSubKey(keypath);
                return key.GetValue(keyname, (object)def).ToString();
            }
            catch
            {
                return def;
            }
        }

        public bool GetRegVal(string keypath, string keyname)
        {
            try
            {
                object def = new object();
                RegistryKey key = rootkey.OpenSubKey(keypath);
                return key.GetValue(keyname, (object)def).ToString().Equals("1") ? true : false;
            }
            catch
            {
                return false;
            }
        }

        public bool SetRegVal(string keypath, string keyname, string keyval)
        {
            try
            {
                RegistryKey key = rootkey.OpenSubKey(keypath, true);
                key.SetValue(keyname, (object)keyval);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public RegistryKey CreateRegKey(string keypath)
        {
            try
            {
                return rootkey.CreateSubKey(keypath);
            }
            catch
            {
                return null;
            }
        }

        public bool DelRegSubKey(string keypath)
        {
            try
            {
                rootkey.DeleteSubKey(keypath);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool DelRegSubKeyTree(string keypath)
        {
            try
            {
                rootkey.DeleteSubKeyTree(keypath);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool DelRegKeyVal(string keypath, string keyname)
        {
            try
            {
                RegistryKey key = rootkey.OpenSubKey(keypath, true);
                key.DeleteValue(keyname);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
} 
